package com.aclass.xqx.classscreen.utlis;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aclass.xqx.classscreen.BaseApplication;
import com.aclass.xqx.classscreen.MainActivity;
import com.aclass.xqx.classscreen.R;
import com.aclass.xqx.classscreen.db.CardStudent;
import com.aclass.xqx.classscreen.db.ReadNews;
import com.aclass.xqx.classscreen.db.StudentNews;
import com.aclass.xqx.classscreen.view.CircleImageView;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by xqx on 2017/11/10.
 *
 */

public class DialogUtlis {
    public static void showStudentDialog(final Context contxt, CardStudent cardStudent,int type) {
        final Dialog  dialog= new Dialog(contxt, R.style.ActionSheetDialogStyle);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //填充对话框的布
        View inflate = LayoutInflater.from(contxt).inflate(R.layout.dialog_studentqd, null);
        TextView tv_time= (TextView) inflate.findViewById(R.id.tv_time);
        TextView tv_Sign= (TextView) inflate.findViewById(R.id.tv_Sign);
        if (type==0){
            tv_Sign.setText("签到成功");
        }else{
            tv_Sign.setText("本地签到成功");
        }

        TextView tv_news= (TextView) inflate.findViewById(R.id.tv_news);
        TextView tv_news1= (TextView) inflate.findViewById(R.id.tv_news1);
        TextView tv_news2= (TextView) inflate.findViewById(R.id.tv_news2);
        TextView tv_newstime= (TextView) inflate.findViewById(R.id.tv_newtime);
        TextView tv_newstime1= (TextView) inflate.findViewById(R.id.tv_newtime1);
        TextView tv_newstime2= (TextView) inflate.findViewById(R.id.tv_newtime2);
        LinearLayout ll_news2= (LinearLayout) inflate.findViewById(R.id.ll_news2);
        LinearLayout ll_news1= (LinearLayout) inflate.findViewById(R.id.ll_news1);
        LinearLayout ll_news= (LinearLayout) inflate.findViewById(R.id.ll_news);
        CircleImageView civ_icon= (CircleImageView) inflate.findViewById(R.id.civ_icon);
        if (!cardStudent.getStudent_url().equals("无头像")) {
            Glide.with(contxt).load(cardStudent.getStudent_url()).error(R.mipmap.student).into(civ_icon);
        }
        tv_time.setText("刷卡时间 "+ClassCalendar.getInttimes());
        //将布局设置给Dialog
        List<ReadNews> readnewses = cardStudent.getReadnewses();
        if (readnewses.size()!=0){
            Log.e("ATG","  read"+readnewses.size());
            AppUtlis.ListSort1(readnewses);
            if (readnewses.size()>2){
                tv_news.setText(readnewses.get(0).getNewsContent()+"  ");
                tv_newstime.setText(readnewses.get(0).getNewsTime());
                Log.e("ATG","时间"+readnewses.get(0).getNewsTime());
                tv_news1.setText(readnewses.get(1).getNewsContent()+"  ");
                tv_newstime1.setText(readnewses.get(1).getNewsTime());
                tv_news2.setText(readnewses.get(2).getNewsContent()+"  ");
                tv_newstime2.setText(readnewses.get(2).getNewsTime());
                ll_news1.setVisibility(View.VISIBLE);
                ll_news.setVisibility(View.VISIBLE);
                ll_news2.setVisibility(View.VISIBLE);
            }else if (readnewses.size()>1){
                tv_news.setText(readnewses.get(0).getNewsContent()+"  ");
                tv_newstime.setText(readnewses.get(0).getNewsTime());
                tv_news1.setText(readnewses.get(1).getNewsContent()+"  ");
                tv_newstime1.setText(readnewses.get(1).getNewsTime());
                ll_news1.setVisibility(View.VISIBLE);
                ll_news.setVisibility(View.VISIBLE);
            }else if(readnewses.size()>0){
                tv_news.setText(readnewses.get(0).getNewsContent()+"  ");
                tv_newstime.setText(readnewses.get(0).getNewsTime());
                ll_news.setVisibility(View.VISIBLE);
            }

        }
        dialog.setContentView(inflate);

        dialog.show();//显示对话框
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        },6000);
    }




    public static void showNewsDialog(Context context,String id,String name,String time,String content){
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //填充对话框的布
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_news, null);
        TextView tv_name= (TextView) inflate.findViewById(R.id.tv_name);
        TextView tv_time= (TextView) inflate.findViewById(R.id.tv_time);
        TextView tv_content= (TextView) inflate.findViewById(R.id.tv_content);
        tv_name.setText(name);
        tv_content.setText(content);
        tv_time.setText(time);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        dialog.show();//显示对话框

        BaseApplication.getDaoInstant().getStudentNewsDao().deleteByKey(id);
        BaseApplication.getDaoInstant().clear();
        PrefUtild.INSTANCE.setString(context,Constant.NEWS,"1");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        },3000);
    }
   static List<StudentNews> classNewses ;
   static int count=0;
    public static void showNewssDialog(final Context context, final String name, final List<StudentNews> list){
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        //填充对话框的布
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_news, null);
        TextView tv_name= (TextView) inflate.findViewById(R.id.tv_name);
        TextView tv_time= (TextView) inflate.findViewById(R.id.tv_time);
        TextView tv_content= (TextView) inflate.findViewById(R.id.tv_content);
        classNewses=list;
        tv_name.setText(name);
        Log.e("ATGSS",""+count);
        tv_content.setText(classNewses.get(count).getNewsContent());
        tv_time.setText(classNewses.get(count).getNewsTime());
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        dialog.show();//显示对话框
        BaseApplication.getDaoInstant().getStudentNewsDao().deleteByKey(classNewses.get(count).getId());
        if (count<classNewses.size()){
            ++count;
            Log.e("ATG",count+"");
        }
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
                if (count!=classNewses.size()) {
                    showNewssDialog(context, name, list);
                }else{
                    count=0;
                    return;
                }
            }
        },3000);

    }

    private static void Dismiss(){

    }
}
